package view{
	
	import control.GameRule;
	
	import event.CustomEvent;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.events.Request;
	
	import view.UIPlayer.ButtonViewLayer;
	import view.UIPlayer.CardViewLayer;
	import view.UIPlayer.LogoView;
	
	public class MainView extends Sprite{
		
		private var logo:LogoView = new LogoView();
		private var btnView:ButtonViewLayer = new ButtonViewLayer();
		private var gameRule:GameRule = new GameRule();
		private var cardView:CardViewLayer = new CardViewLayer();
		
		public function MainView(){
			init();
		}
		
		private function init():void{
			var loadBackground:Loader = new Loader();
			loadBackground.load(new URLRequest("assets/images/backgound.jpg"));
			addChild(loadBackground);
			logo.addEventListener(LogoView.LOGO,putLogo);
			trace("加载LOGO完成");
			
			addChild(btnView);
			addChild(cardView);
			
			btnView.addEventListener(CustomEvent.ONCLIK_START,btnStartHandler);
			btnView.addEventListener(CustomEvent.ONCLIK_SHOW,btnEndHandler);		
			
			gameRule.addEventListener(CustomEvent.GAME_DISPLAY,gameDisplayHandler);
			gameRule.addEventListener(CustomEvent.GAME_DISPLAY2,gameDisplay2Handler);
		}
		
		private function putLogo(e:Event):void{
			addChild(logo);
			logo.x = 100;
			logo.y = 563;
			logo.scaleX = 1.8;
			logo.scaleY = 1.8;
		}
		
		private function btnStartHandler(e:CustomEvent):void{
			if(e.BTNState == 1){
				gameRule.startGame();
			}else if(e.BTNState == 2){
				gameRule.gameGetCard();
			}else{
				
			}
			
		}
		
		private function btnEndHandler(e:CustomEvent):void{
			gameRule.gameEnd();
		}
		
		private function gameDisplayHandler(e:CustomEvent):void{
			btnView.updateGameStart2();
			cardView.displayCard();
		}
		
		private function gameDisplay2Handler(e:CustomEvent):void{
			cardView.displayCard();
		}
		
	}
}